package com.qf.springboot230203.service.impl;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.springboot230203.dao.OnePieceDao;
import com.qf.springboot230203.entity.OnePiece;
import com.qf.springboot230203.ex.OnepieceException;
import com.qf.springboot230203.service.OnePieceService;
import com.qf.springboot230203.vo.OnePiece2VO;
import com.qf.springboot230203.vo.OnePpieceVo;
import com.qf.springboot230203.vo.PagingVo;
import com.qf.springboot230203.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class OnePieceServiceImpl implements OnePieceService {

    @Autowired
    OnePieceDao onePieceDao;

    @Value("${qfsy.fileupload.path}")
    String filepa;

    @Override
    public OnePiece login(String username, String password) {
        //1.根据用户名查询用户信息
        OnePiece onePiece = onePieceDao.getOnePieceName(username);
        if (onePiece == null){
            return null;
        }else {
            //2.比对密码

            String upassword = onePiece.getPassword();
            String md5Password = DigestUtil.md5Hex(password);
            if (upassword.equals(md5Password)){
                return onePiece;
            }
            return null;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertOnePiece(OnePpieceVo onePpieceVo) throws IOException {
        String prefix = UUID.randomUUID().toString();
        //拿原始文件名
        String pof = onePpieceVo.getPhoto().getOriginalFilename();
        String fileName = prefix + "_" + pof;
        //文件路径
        String f = filepa + fileName;
        File file1 = new File(f);
        onePpieceVo.getPhoto().transferTo(file1);
        String md5Hex = DigestUtil.md5Hex(onePpieceVo.getPassword1());
        OnePiece onePiece = new OnePiece(onePpieceVo.getUsername(), md5Hex, onePpieceVo.getSex(), onePpieceVo.getDate(), new Date(), fileName);
        onePieceDao.insertOnePiece(onePiece);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public OnePiece uname(String username) {
        OnePiece onePieceName = onePieceDao.getOnePieceName(username);
        if (onePieceName == null){
            return null;
        }
        return onePieceName;
    }

    @Override
    public TableVO<OnePiece> getAllLimtit(PagingVo pagingVo) {


        PageHelper.startPage(pagingVo.getPage(),pagingVo.getLimit());
        List<OnePiece> allLimit = onePieceDao.getAllone(pagingVo);

        PageInfo<OnePiece> pageInfo = new PageInfo<>(allLimit);
        int count = (int) pageInfo.getTotal();
        TableVO tableVO = TableVO.ok(count,allLimit);
        return tableVO;
    }


    /**
     * 添加数据
     * @param vo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addInsert(OnePiece2VO vo) throws OnepieceException {
        OnePiece onePieceName = onePieceDao.getOnePieceName(vo.getName());
        if (onePieceName != null){
            throw new OnepieceException("1001","用户名重复");
        }
        //密码处理
        String md5Hex = DigestUtil.md5Hex(vo.getPassword());
        //落库
        onePieceDao.insertOnePiece(new OnePiece(vo.getName(),md5Hex,vo.getSex()
                ,vo.getBirthday(),vo.getRegistTime(),null));
    }

    /**
     * 删除
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delId(Integer[] ids) {

        onePieceDao.del(ids);

    }

    /**
     * 根据id修改用户信息
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updata(OnePiece onePiece) throws OnepieceException {
        Integer id = onePiece.getId();
        if (id == null){
            throw new OnepieceException("-1002","id不存在");
        }

        String password = onePiece.getPassword();
        OnePiece onePiece1 = onePieceDao.selectBiId(onePiece.getId());
        String password1 = onePiece1.getPassword();
        if (!password1.equals(password)){
            String md5Hex = DigestUtil.md5Hex(password);
            if (md5Hex.equals(password1)){
                throw new OnepieceException("-1003","俩次密码不一样");
            }else {
                onePiece.setPassword(md5Hex);
            }
        }

        onePieceDao.update(onePiece);
    }

    @Override
    public String getAllOnePiece(String filepath) {
        List<OnePiece> allOnePiece = onePieceDao.getAllOnePiece();
        String fileName = "onepiece.csv";
        FileWriter fw = new FileWriter(filepath+fileName);
        //清楚文件数据
        fw.write("");
        for (OnePiece onePiece : allOnePiece) {
            String string = onePiece.toCSVString();
            fw.append(string+"\r\n");
        }

        return fileName;
    }

    @Override
    public void insertAll(OnePiece onePiece) {
        onePieceDao.insertOnePiece(onePiece);
    }


}
